from django.shortcuts import redirect
from django.utils.deprecation import MiddlewareMixin


class AuthMiddleware(MiddlewareMixin):
    """中间件1"""

    def process_request(self, request):
        # 排除不需要登录的url
        if request.path_info in ["/login/", "/image/code/"]:
            return None
        info_dict = request.session.get("info")
        if info_dict:
            return None
        # 未登录，重定向到登录页面
        return redirect('/login/')

    def process_response(self, request, response):
        return response
